package Core;

import Core.ControlImage;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Alexander Naumow
 */

public class Control extends GUIObject
{
	public static final int INACTIVE=0, ACTIVE=1, PRESSED=2;
	public static final int LEFTOF=1, RIGHTOF=2, THIS=4, UPPER=5, UNDER=6;

	public boolean lockOn = false;
	protected Form parentForm;
	protected LayoutManager parentManager;

	protected String selectedAction = " ";
	protected String pressedAction = " ";

	protected  int state = INACTIVE;
	protected  String caption = "";

	protected boolean selectable = true;

	private boolean allowDeselect = false;

	protected Vector images = new Vector();
	public static int imagesBorder = 2;

	public String	getCaption(){ return caption; }
	public void		setCaption(String caption){ this.caption = caption; }

	protected void setActionText(String text)
	{
		if(parentForm != null) parentForm.setActionText(text);
	}

	public void setParentForm(Form f)
	{
		if(f != null) parentForm = f;
	}

	public void setParentManager(LayoutManager l)
	{
		if(l != null) parentManager = l;
	}
	
	public Control(String name, String caption, int w, int h)
	{
		this.name = name;
		this.caption = caption;
		W = w;
		H = h;
	}

	public Control(int w, int h)
	{
		W = w;
		H = h;
	}

	public Control()
	{
		W = 0;
		H = 0;
	}

	public boolean select(int what)
	{
		//if(what != THIS) return false;
		if(parentForm != null && state != ACTIVE) parentForm.onEvent(GM.CONTROL_SELECTED, this);
		setActionText(selectedAction);
		state = ACTIVE;
		return true;
	}

	public boolean deselect()
	{
		state = INACTIVE;
		return true;
	}

	public void press()
	{
		if(lockOn) setActionText(pressedAction);
		state = PRESSED;
	}

	public void release()
	{
		if(lockOn) setActionText(selectedAction);
		state = ACTIVE;
	}

	public void repeate()
	{
		state = PRESSED;
	}

	public int	getState() { return state; }

	public void draw(Graphics g, int x, int y)
	{
		if(!images.isEmpty())
		{
			ControlImage bi;
			int ix,iy;

			for(int i=0; i<images.size(); i++)
			{
				bi = (ControlImage) images.elementAt(i);

				ix=x+(getW() - bi.getW())/2;
				iy=y+(getH() - bi.getH())/2;

				if(binFlag(bi.align, ALIGN.LEFT))	ix=x+imagesBorder;
				if(binFlag(bi.align, ALIGN.RIGHT))	ix=x+getW()-bi.getW()-imagesBorder;
				if(binFlag(bi.align, ALIGN.TOP))	iy=y+imagesBorder;
				if(binFlag(bi.align, ALIGN.BOTTOM))	iy=y+getH()-bi.getH()-imagesBorder;
				if(binFlag(bi.align, ALIGN.CENTER))
				{
					ix=x+(getW() - bi.getW())/2;
					iy=y+(getH() - bi.getH())/2;
				}
				if(state == PRESSED) {ix++; iy++;}

				bi.draw(g, ix, iy);
			}
		}
	}

	public void processKey(int key, int state)
	{
		if(lockOn)
		{
			if(key == Key.P_KEY && state==Key.RELEASED && getState()==Control.PRESSED && allowDeselect)
			{
				release();
				if(parentForm != null) parentForm.onEvent(GM.CONTROL_RELEASED, this);
				allowDeselect = false;
				return;
			}
			allowDeselect = true;
		}
	}
	
	public void addImage(ControlImage bi)
	{
		if(bi != null) images.addElement(bi);
	}

	public void removeImage(ControlImage bi)
	{
		if(bi != null) images.removeElement(bi);
	}

	public void removeAllImages()
	{
		if(!images.isEmpty()) images.removeAllElements();
	}
}

